<?php
defined('BASEPATH') OR exit('No direct script access allowed');
set_time_limit(0);

class Index extends CI_Controller {

    protected $data;
    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->data['base_url'] = $this->config->config['base_url'];
        $this->load->model('prize');
        $this->load->model('tuan');
        $this->load->model('cartuan');
        $this->load->library('jssdk');
    }


	public function index()
	{
        //生成微信url
        $this->data['film'] = $this->getUrl('film');
        $this->load->view('m/index',$this->data);
	}
    public function lucky()
    {
        $now_hour = date('H');
//        if($now_hour < 10){
            header('location:'.$this->data['base_url'].'/index.php/m/index/over');
//        }
        $prize = $this->prize->get_prize_info();
        if(empty($prize)){
            header('location:'.$this->data['base_url'].'/index.php/m/index/gameover');
        }
        $data = $this->jssdk->getSignPackage();
        $this->data['signPackage'] = $data;
        $mobile = isset($_GET['mobile']) && strlen($_GET['mobile']) ? $_GET['mobile'] : null;
        if($mobile !== null){
            $counts = $this->prize->get_user_prize_counts($mobile);
            $user = $this->prize->get_users_nums($mobile);
            $cj_counts = !empty($user) ? $user['num'] : 1;
            $this->data['nums'] = $cj_counts - $counts;
        }else{
            $this->data['nums'] = 1;
        }
        $this->load->view('m/lucky',$this->data);
    }

    public function noopen()
    {
        $this->load->view('m/noopen',$this->data);
    }

    public function gameover()
    {
        $this->load->view('m/gameover',$this->data);
    }

    public function over()
    {
        $this->load->view('m/over',$this->data);
    }


    public function film()
    {
        header('location:'.$this->data['base_url'].'/index.php/m/index/over');
        return;
        if(!isset($_GET['code']))
        {
            //生成微信url
            $url = $this->getUrl('film');
            header('location:'.$url);
        }
        $code = $_GET['code'];
        $user = $this->jssdk->getUserInfo($code);
        if(isset($user['errcode']))
        {
            //生成微信url
            $url = $this->getUrl('film');
            header('location:'.$url);
        }

        $mobile = '';
        $weixin =  $this->tuan->get_weixin_mobile(array('open_id'=>$user['openid']));
//        $weixin =  $this->tuan->get_weixin_mobile(array('open_id'=> "o7CY4uE8vb03RndPS2YhQ0fyevG0"));

        $tuanData =  $this->tuan->get_tuan(array('status' => 1), 'count(*) as count');
        $sy = 888 - ($tuanData[0]['count']*8);

        $tuanYuans = array();
        $is_tuan = 0;
        $guoqi  = 0;
        $cg = 0;
        $tz_mobile = '';
        if(!empty($weixin)){
            $mobile = $weixin['mobile'];
//            $open_id = "o7CY4uE8vb03RndPS2YhQ0fyevG0";
            $open_id = $user['openid'];
            $res = $this->tuan->get_tuan_member(array('tuanyuan.open_id' =>$open_id));
            if( strtotime($res[0]['create_time'])>= (time()-24*3600))
            {
                $tz_info = $this->tuan->get_weixin_mobile(array('open_id'=>$res[0]['open_id']));
                $tz_mobile = $tz_info['mobile'];
                $open_ids = $this->tuan->get_tuanyuans(array('tuan_id'=>$res[0]['id']));
                foreach($open_ids as $oid)
                {
                    $tuanYuans[] =  $this->tuan->get_weixin_mobile(array('open_id'=>$oid['open_id']));
                }
                $is_tuan = 1;
            }else{
                $guoqi  = 1;
            }

            if($res[0]['status'] == 1)
            {
                $cg = 1;
                $is_tuan = 1;
            }
        }
        $this->data['tz_mobile'] = $tz_mobile;
        $this->data['tuanYuans'] = $tuanYuans;
        $this->data['sy'] = $sy;
        $this->data['user'] = $user;
        $this->data['mobile'] = $mobile;
        $this->data['is_tuan'] = $is_tuan;
        $this->data['guoqi'] = $guoqi;
        $this->data['cg'] = $cg;
        $this->load->view('m/film',$this->data);

    }
    /**
     * 建团
     */
    public function build_tuan()
    {
        $open_id = isset($_GET['open_id']) && strlen($_GET['open_id']) ? $_GET['open_id'] : null;
        $mobile = isset($_GET['mobile']) && strlen($_GET['mobile']) && $this->checkmobile($_GET['mobile']) ? $_GET['mobile'] : null;
        $nickname = isset($_GET['nickname']) && strlen($_GET['nickname']) ? $_GET['nickname'] : null;
        $headimgurl = isset($_GET['headimgurl']) && strlen($_GET['headimgurl'])  ? $_GET['headimgurl'] : null;
        if($open_id === null){
            $data['code'] = 0;
            $data['data'] = 'id不能为空';

            echo json_encode($data);
            exit;
        }
        $user = $this->tuan->get_weixin_mobile(array('open_id' => $open_id));
        if(empty($user)){
            if($mobile === null){
                $data['code'] = 0;
                $data['data'] = 'mobile不能为空';

                echo json_encode($data);
                exit;
            }
            //判断其用户是否使用过该手机
            $check_mobile = $this->tuan->get_weixin_mobile(array('mobile' => $mobile));
            if(!empty($check_mobile)){
                $data['code'] = 4;
                $data['data'] = '其他微信用户已使用过该手机号';

                echo json_encode($data);
                exit;
            }
        }
        //建团成功
        $res = $this->tuan->get_tuan(array('tuan.open_id' => $open_id, 'tuan.status' => 1));
        //今日建团
        $res2 = $this->tuan->get_tuan(array('tuan.open_id' => $open_id, 'UNIX_TIMESTAMP(tuan.create_time) >= ' => time()-24*3600));
        if(!$res && !$res2){
            $res1 = $this->tuan->get_tuan(array('tuan.status' => 1));
            if(count($res1) == 111){
                $data['code'] = 3;
                $data['data'] = '建团数量已满，活动结束！';

                echo json_encode($data);
                exit;
            }

            //建团流程
            $tuan_id = $this->tuan->insert_tuan(array('open_id' => $open_id));
            if($tuan_id){
                if(empty($user)){
                    $empty_tuan = true;
                    $user_data = array('open_id' => $open_id, 'mobile' => $mobile, 'headimgurl' => $headimgurl, 'nickname' => $nickname);
                }else{
                    $empty_tuan = false;
                }
                $this->tuan->insert_tuanyuan($tuan_id,array('open_id' => $open_id, 'tuan_id' => $tuan_id),$empty_tuan,$user_data);

                $data['code'] = 1;
                $count = $this->tuan->get_tuan_member(array('tuan.id' => $tuan_id),'weixin_mobile.*');
                $user = $this->tuan->get_weixin_mobile(array('open_id' =>$open_id));
                $info['count'] = count($count);
                $html = '';
                if($count){
                    foreach($count as $val){
                        $html .=' <li><div class="txbox"> <img src="'.$val['headimgurl'].'" alt=""><i></i></div> </li>';
                    }
                }
                $info['headimgurl'] =$html;
                $info['mobile'] = $user['mobile'];
                $data['data'] = $info;

                echo json_encode($data);
                exit;
            }else{
                $data['code'] = 0;
                $data['data'] = '建团失败';

                echo json_encode($data);
                exit;
            }
        }else{
            $data['code'] = 2;
            $data['data'] = '已经建过团';

            echo json_encode($data);
            exit;
        }
    }

    /**
     * 参团
     */
    public function join_tuan()
    {
        $tz_mobile = isset($_GET['tz_mobile']) && strlen($_GET['tz_mobile']) && $this->checkmobile($_GET['tz_mobile']) ? $_GET['tz_mobile'] : null;//团长手机号
        $open_id = isset($_GET['open_id']) && strlen($_GET['open_id']) ? $_GET['open_id'] : null;
        $mobile = isset($_GET['mobile']) && strlen($_GET['mobile']) && $this->checkmobile($_GET['mobile']) ? $_GET['mobile'] : null;
        $nickname = isset($_GET['nickname']) && strlen($_GET['nickname']) ? $_GET['nickname'] : null;
        $headimgurl = isset($_GET['headimgurl']) && strlen($_GET['headimgurl'])  ? $_GET['headimgurl'] : null;
        if($open_id === null || $tz_mobile === null){
            $data['code'] = 0;
            $data['data'] = 'id,团长手机号不能为空';

            echo json_encode($data);
            exit;
        }
        $user = $this->tuan->get_weixin_mobile(array('open_id' => $open_id));
        if(empty($user)){
            if($mobile === null){
                $data['code'] = 0;
                $data['data'] = 'mobile不能为空';

                echo json_encode($data);
                exit;
            }
            //判断其用户是否使用过该手机
            $check_mobile = $this->tuan->get_weixin_mobile(array('mobile' => $mobile));
            if(!empty($check_mobile)){
                $data['code'] = 4;
                $data['data'] = '其他微信用户已使用过该手机号';

                echo json_encode($data);
                exit;
            }
        }
        //已参过成功的团
        $res = $this->tuan->get_tuan_member(array('tuanyuan.open_id' => $open_id, 'tuan.status' => 1));
        //今日参的团
        $res2 = $this->tuan->get_tuan_member(array('tuanyuan.open_id' => $open_id, 'UNIX_TIMESTAMP(tuan.create_time) >= ' => time()-24*3600));

        if(empty($res2) && empty($res)){
            $res1 = $this->tuan->get_tuan(array('tuan.status' => 1));
            if(count($res1) == 111){
                $data['code'] = 3;
                $data['data'] = '建团数量已满，活动结束！';

                echo json_encode($data);
                exit;
            }
            //判断该团是否已满或真实存在
            $res3 = $this->tuan->get_tuan(array('weixin_mobile.mobile' => $tz_mobile,  'UNIX_TIMESTAMP(tuan.create_time) >= ' => time()-24*3600, 'status' => 0),'tuan.*,weixin_mobile.mobile');

            if(empty($res3)){
                $data['code'] = 5;
                $data['data'] = '该团已结束或不存在';

                echo json_encode($data);
                exit;
            }else{
                if(empty($user)){
                  $empty_tuan = true;
                    $user_data = array('open_id' => $open_id, 'mobile' => $mobile, 'headimgurl' => $headimgurl, 'nickname' => $nickname);
                }else{
                    $empty_tuan = false;
                }
                $tuan_id = $res3[0]['id'];
                $r = $this->tuan->insert_tuanyuan($tuan_id,array('open_id' => $open_id, 'tuan_id' => $tuan_id),$empty_tuan,$user_data);
                if(!$r){
                    $data['code'] = 6;
                    $data['data'] = '人数已满';

                    echo json_encode($data);
                    exit;
                }

                $data['code'] = 1;
                $count = $this->tuan->get_tuan_member(array('tuan.id' => $tuan_id),'weixin_mobile.*');
                $user = $this->tuan->get_weixin_mobile(array('open_id' =>$res3[0]['open_id']));
                $info['count'] = count($count);
                $html = '';
                if($count){
                    foreach($count as $val){
                        $html .=' <li><div class="txbox"> <img src="'.$val['headimgurl'].'" alt=""><i></i></div> </li>';
                    }
                }
                $info['headimgurl'] =$html;
                $info['mobile'] = $user['mobile'];
                $data['data'] = $info;

                echo json_encode($data);
                exit;
            }
        }else{
            $data['code'] = 2;
            $data['data'] = '已参过团';

            echo json_encode($data);
            exit;
        }
    }

    public function car()
    {
        $tuanData =  $this->cartuan->get_cartuan(array('status' => 1), 'count(*) as count');
        $sy = 888 - ($tuanData[0]['count']*8);
        $this->data['sy'] = $sy;
        $this->load->view('m/car_buy',$this->data);
//        $this->load->view('m/weikaishi',$this->data);
//        $this->load->view('m/car',$this->data);
    }
    public function test()
    {

        if(!isset($_GET['code']))
        {
            //生成微信url
            $url = $this->getUrl('test');
            header('location:'.$url);
        }
        $code = $_GET['code'];
        $user = $this->jssdk->getUserInfo($code);
        if(isset($user['errcode']))
        {
            //生成微信url
            $url = $this->getUrl('test');
            header('location:'.$url);
        }

        $mobile = '';
        $weixin =  $this->cartuan->get_weixin_mobile(array('open_id'=>$user['openid']));
//        $weixin =  $this->cartuan->get_weixin_mobile(array('open_id'=> "o7CY4uE8vb03RndPS2YhQ0fyevG2"));
        $tuanData =  $this->cartuan->get_cartuan(array('status' => 1), 'count(*) as count');

        $sy = 888 - ($tuanData[0]['count']*8);

        $tuanYuans = array();
        $is_tuan = 0;
        $guoqi  = 0;
        $cg = 0;
        $tz_mobile = '';
        if(!empty($weixin)){
            $mobile = $weixin['mobile'];
//            $open_id = "o7CY4uE8vb03RndPS2YhQ0fyevG2";
            $open_id = $user['openid'];
            $res = $this->cartuan->get_cartuan_member(array('cartuanyuan.open_id' =>$open_id));
            if( strtotime($res[0]['create_time'])>= (time()-24*3600))
            {
                $tz_info = $this->cartuan->get_weixin_mobile(array('open_id'=>$res[0]['open_id']));
                $tz_mobile = $tz_info['mobile'];
                if($open_id != $res[0]['open_id'])
                {
                    $is_tz = 0;
                }else{
                    $is_tz = 1;
                }
                $open_ids = $this->cartuan->get_cartuanyuans(array('tuan_id'=>$res[0]['id']));
                foreach($open_ids as $oid)
                {
                    $tuanYuans[] =  $this->cartuan->get_weixin_mobile(array('open_id'=>$oid['open_id']));
                }
                $is_tuan = 1;
            }else{
                $guoqi  = 1;
            }

            if($res[0]['status'] == 1)
            {
                $cg = 1;
                $is_tuan = 1;
            }
        }
        $this->data['tz_mobile'] = $tz_mobile;
        $this->data['tuanYuans'] = $tuanYuans;
        $this->data['sy'] = $sy;
        $this->data['user'] = $user;
        $this->data['mobile'] = $mobile;
        $this->data['is_tuan'] = $is_tuan;
        $this->data['guoqi'] = $guoqi;
        $this->data['cg'] = $cg;
        $this->load->view('m/car',$this->data);
    }

    public function car_buy()
    {
        $tuanData =  $this->cartuan->get_cartuan(array('status' => 1), 'count(*) as count');
        $sy = 888 - ($tuanData[0]['count']*8);
        $this->data['sy'] = $sy;
        $this->load->view('m/car_buy',$this->data);
//        $this->load->view('m/car',$this->data);
    }


    public function film1()
    {
        if(!isset($_GET['code']))
        {
            //生成微信url
            $url = $this->getUrl('film');
            header('location:'.$url);
        }
        $code = $_GET['code'];
        $user = $this->jssdk->getUserInfo($code);
        if(isset($user['errcode']))
        {
            //生成微信url
            $url = $this->getUrl('film');
            header('location:'.$url);
        }

        $mobile = '';
        $weixin =  $this->cartuan->get_weixin_mobile(array('open_id'=>$user['openid']));
//        $weixin =  $this->tuan->get_weixin_mobile(array('open_id'=> "o7CY4uE8vb03RndPS2YhQ0fyevG0"));

        $tuanData =  $this->cartuan->get_cartuan(array('status' => 1), 'count(*) as count');
        $sy = 888 - ($tuanData[0]['count']*8);

        $tuanYuans = array();
        $is_tuan = 0;
        $guoqi  = 0;
        $cg = 0;
        $tz_mobile = '';
        if(!empty($weixin)){
            $mobile = $weixin['mobile'];
//            $open_id = "o7CY4uE8vb03RndPS2YhQ0fyevG0";
            $open_id = $user['openid'];
            $res = $this->cartuan->get_cartuan_member(array('cartuanyuan.open_id' =>$open_id));
            if( strtotime($res[0]['create_time'])>= (time()-24*3600))
            {
                $tz_info = $this->cartuan->get_weixin_mobile(array('open_id'=>$res[0]['open_id']));
                $tz_mobile = $tz_info['mobile'];
                $open_ids = $this->cartuan->get_cartuanyuans(array('tuan_id'=>$res[0]['id']));
                foreach($open_ids as $oid)
                {
                    $tuanYuans[] =  $this->cartuan->get_weixin_mobile(array('open_id'=>$oid['open_id']));
                }
                $is_tuan = 1;
            }else{
                $guoqi  = 1;
            }

            if($res[0]['status'] == 1)
            {
                $cg = 1;
                $is_tuan = 1;
            }
        }
        $this->data['tz_mobile'] = $tz_mobile;
        $this->data['tuanYuans'] = $tuanYuans;
        $this->data['sy'] = $sy;
        $this->data['user'] = $user;
        $this->data['mobile'] = $mobile;
        $this->data['is_tuan'] = $is_tuan;
        $this->data['guoqi'] = $guoqi;
        $this->data['cg'] = $cg;
        $this->load->view('m/film',$this->data);

    }
    /**
     * 建团
     */
    public function car_build_tuan()
    {
        $open_id = isset($_GET['open_id']) && strlen($_GET['open_id']) ? $_GET['open_id'] : null;
        $mobile = isset($_GET['mobile']) && strlen($_GET['mobile']) && $this->checkmobile($_GET['mobile']) ? $_GET['mobile'] : null;
        $nickname = isset($_GET['nickname']) && strlen($_GET['nickname']) ? $_GET['nickname'] : null;
        $headimgurl = isset($_GET['headimgurl']) && strlen($_GET['headimgurl'])  ? $_GET['headimgurl'] : null;
        if($open_id === null){
            $data['code'] = 0;
            $data['data'] = 'id不能为空';

            echo json_encode($data);
            exit;
        }
        $user = $this->cartuan->get_weixin_mobile(array('open_id' => $open_id));
        if(empty($user)){
            if($mobile === null){
                $data['code'] = 0;
                $data['data'] = 'mobile不能为空';

                echo json_encode($data);
                exit;
            }
            //判断其用户是否使用过该手机
            $check_mobile = $this->cartuan->get_weixin_mobile(array('mobile' => $mobile));
            if(!empty($check_mobile)){
                $data['code'] = 4;
                $data['data'] = '其他微信用户已使用过该手机号';

                echo json_encode($data);
                exit;
            }
        }
        //建团成功
        $res = $this->cartuan->get_cartuan(array('cartuan.open_id' => $open_id, 'cartuan.status' => 1));
        //今日建团
        $res2 = $this->cartuan->get_cartuan(array('cartuan.open_id' => $open_id, 'UNIX_TIMESTAMP(cartuan.create_time) >= ' => time()-24*3600));
        if(!$res && !$res2){
            $res1 = $this->cartuan->get_cartuan(array('cartuan.status' => 1));
            if(count($res1) == 111){
                $data['code'] = 3;
                $data['data'] = '建团数量已满，活动结束！';

                echo json_encode($data);
                exit;
            }

            //建团流程
            $tuan_id = $this->cartuan->insert_cartuan(array('open_id' => $open_id));
            if($tuan_id){
                if(empty($user)){
                    $empty_tuan = true;
                    $user_data = array('open_id' => $open_id, 'mobile' => $mobile, 'headimgurl' => $headimgurl, 'nickname' => $nickname);
                }else{
                    $empty_tuan = false;
                }
                $this->cartuan->insert_cartuanyuan($tuan_id,array('open_id' => $open_id, 'tuan_id' => $tuan_id),$empty_tuan,$user_data);

                $data['code'] = 1;
                $count = $this->cartuan->get_cartuan_member(array('cartuan.id' => $tuan_id),'car_weixin_mobile.*');
                $user = $this->cartuan->get_weixin_mobile(array('open_id' =>$open_id));
                $info['count'] = count($count);
                $html = '';
                if($count){
                    foreach($count as $val){
                        $html .=' <li><div class="txbox"> <img src="'.$val['headimgurl'].'" alt=""><i></i></div> </li>';
                    }
                }
                $info['headimgurl'] =$html;
                $info['mobile'] = $user['mobile'];
                $data['data'] = $info;

                echo json_encode($data);
                exit;
            }else{
                $data['code'] = 0;
                $data['data'] = '建团失败';

                echo json_encode($data);
                exit;
            }
        }else{
            $data['code'] = 2;
            $data['data'] = '已经建过团';

            echo json_encode($data);
            exit;
        }
    }

    /**
     * 参团
     */
    public function car_join_tuan()
    {
        $tz_mobile = isset($_GET['tz_mobile']) && strlen($_GET['tz_mobile']) && $this->checkmobile($_GET['tz_mobile']) ? $_GET['tz_mobile'] : null;//团长手机号
        $open_id = isset($_GET['open_id']) && strlen($_GET['open_id']) ? $_GET['open_id'] : null;
        $mobile = isset($_GET['mobile']) && strlen($_GET['mobile']) && $this->checkmobile($_GET['mobile']) ? $_GET['mobile'] : null;
        $nickname = isset($_GET['nickname']) && strlen($_GET['nickname']) ? $_GET['nickname'] : null;
        $headimgurl = isset($_GET['headimgurl']) && strlen($_GET['headimgurl'])  ? $_GET['headimgurl'] : null;
        if($open_id === null || $tz_mobile === null){
            $data['code'] = 0;
            $data['data'] = 'id,团长手机号不能为空';

            echo json_encode($data);
            exit;
        }
        $user = $this->cartuan->get_weixin_mobile(array('open_id' => $open_id));
        if(empty($user)){
            if($mobile === null){
                $data['code'] = 0;
                $data['data'] = 'mobile不能为空';

                echo json_encode($data);
                exit;
            }
            //判断其用户是否使用过该手机
            $check_mobile = $this->cartuan->get_weixin_mobile(array('mobile' => $mobile));
            if(!empty($check_mobile)){
                $data['code'] = 4;
                $data['data'] = '其他微信用户已使用过该手机号';

                echo json_encode($data);
                exit;
            }
        }
        //已参过成功的团
        $res = $this->cartuan->get_cartuan_member(array('cartuanyuan.open_id' => $open_id, 'cartuan.status' => 1));
        //今日参的团
        $res2 = $this->cartuan->get_cartuan_member(array('cartuanyuan.open_id' => $open_id, 'UNIX_TIMESTAMP(cartuan.create_time) >= ' => time()-24*3600));

        if(empty($res2) && empty($res)){
            $res1 = $this->cartuan->get_cartuan(array('cartuan.status' => 1));
            if(count($res1) == 111){
                $data['code'] = 3;
                $data['data'] = '建团数量已满，活动结束！';

                echo json_encode($data);
                exit;
            }
            //判断该团是否已满或真实存在
            $res3 = $this->cartuan->get_cartuan(array('car_weixin_mobile.mobile' => $tz_mobile,  'UNIX_TIMESTAMP(cartuan.create_time) >= ' => time()-24*3600),'cartuan.*,car_weixin_mobile.mobile');
//            var_dump($res3);die;
            if(empty($res3)){
                $data['code'] = 5;
                $data['data'] = '该团不存在';

                echo json_encode($data);
                exit;
            }else{
                if($res3[0]['status'])
                {
                    $data['code'] = 6;
                    $data['data'] = '人数已满';

                    echo json_encode($data);
                    exit;
                }
                if(empty($user)){
                    $empty_tuan = true;
                    $user_data = array('open_id' => $open_id, 'mobile' => $mobile, 'headimgurl' => $headimgurl, 'nickname' => $nickname);
                }else{
                    $empty_tuan = false;
                }
                $tuan_id = $res3[0]['id'];
                $r = $this->cartuan->insert_cartuanyuan($tuan_id,array('open_id' => $open_id, 'tuan_id' => $tuan_id),$empty_tuan,$user_data);

                if(!$r){
                    $data['code'] = 6;
                    $data['data'] = '人数已满';

                    echo json_encode($data);
                    exit;
                }


                $data['code'] = 1;
                $count = $this->cartuan->get_cartuan_member(array('cartuan.id' => $tuan_id),'car_weixin_mobile.*');
                $user = $this->cartuan->get_weixin_mobile(array('open_id' =>$res3[0]['open_id']));
                $info['count'] = count($count);
                $html = '';
                if($count){
                    foreach($count as $val){
                        $html .=' <li><div class="txbox"> <img src="'.$val['headimgurl'].'" alt=""><i></i></div> </li>';
                    }
                }
                $info['headimgurl'] =$html;
                $info['mobile'] = $user['mobile'];
                $data['data'] = $info;

                echo json_encode($data);
                exit;
            }
        }else{
            $data['code'] = 2;
            $data['data'] = '已参过团';

            echo json_encode($data);
            exit;
        }
    }






    /**
     * 抽奖
     */
    public function getnumbers()
    {
        $mobile = isset($_POST['mobile']) && strlen($_POST['mobile']) ? $_POST['mobile'] : null;
        if($mobile === null || !$this->checkmobile($mobile)){
            $data['code'] = 0;
            $data['data'] = '手机号码错误';

            echo json_encode($data);
            exit;
        }

        $prize = $this->prize->get_prize_info();
        if($prize){
            $result = $this->prize->get_prize($mobile);
            if($result !== false){
                $a = 8;
                if($result == 0){
                    $rand_num = array($this->get_rand(),$this->get_rand(),$this->get_rand());
                    shuffle($rand_num);
                    $data['data'] = $rand_num;
                    $data['type'] = 1;
                }elseif($result == 1){
                    $rand_num = array($a,$this->get_rand(),$this->get_rand());
                    shuffle($rand_num);
                    $data['data'] = $rand_num;
                    $data['type'] = 8;
                }elseif($result == 2){
                    $rand_num = array($a,$a,$this->get_rand());
                    shuffle($rand_num);
                    $data['data'] = $rand_num;
                    $data['type'] = 16;
                }
                $counts = $this->prize->get_user_prize_counts($mobile);
                $user = $this->prize->get_users_nums($mobile);
                $cj_counts = !empty($user) ? $user['num'] : 1;
                $data['nums'] = $cj_counts - $counts;
                $data['num'] = 3 - $counts;
                $data['code'] = 1;

                echo json_encode($data);
                exit;

            }else{
                $counts = $this->prize->get_user_prize_counts($mobile);
                $data['code'] = 2;
                $data['num'] = $counts;
                $data['data'] = '已经抽过奖啦';
                echo json_encode($data);
                exit;
            }
        }else{
            $counts = $this->prize->get_user_prize_counts($mobile);
            $data['code'] = 3;
            $data['num'] = $counts;
            $data['data'] = '抽奖结束';
            echo json_encode($data);
            exit;
        }

    }

    public function checkmobile($mobile)
    {
        if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/",$mobile)){
            //验证通过
            return true;
        }else{
            //手机号码格式不对
            return false;
        }
    }

    public function savenumbers()
    {
        $mobile = isset($_POST['mobile']) && strlen($_POST['mobile']) ? $_POST['mobile'] : null;
        $num = isset($_POST['num']) && strlen($_POST['num']) ? $_POST['num'] : null;
        if(empty($mobile) || !$this->checkmobile($mobile))
        {
            $data['code'] = 0;
            $data['data'] = '';
            echo json_encode($data);
            exit;
        }
        $d = $this->prize->get_users_nums($mobile);
        if(empty($d))
        {
            $d['mobile'] = $mobile;
            $d['num'] = $num + 1;
        }else{
            if($d['num']>2)
            {
                $data['code'] = 0;
                $data['data'] = '';
                echo json_encode($data);
                exit;

            }

            $d['num'] =  $d['num'] +$num ;
            unset($d['modify_time']);
        }
        $re = $this->prize->save_user_nums($d);
        if($re)
        {
            $data['code'] = 1;
            $data['data'] = '';
        }
        echo json_encode($data);
        exit;
    }


    /**
     * 获取非8随机数
     *
     * @param $num
     * @return int
     */
    protected function get_rand(){
        do{
            $num = rand(1,9);
        }while($num == 8);
        return $num;
    }

    /**
     * @param $action
     * @return string
     * 生成微信回调url
     */
    public function getUrl($action)
    {
        $redirectUri = "http://8ZN.js118114.com/WeiXin/cms/index.php/m/index/{$action}";
        $redirectUri  =  urlencode($redirectUri);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->jssdk->appId}&redirect_uri={$redirectUri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
    }

    function  getWeiXinData()
    {
        $re['appId'] = 'wx6bf989298d63204d';
        $re['secret'] = 'd9982f926525d1e1c7496d21adec05fa';
        $re['timestamp'] = (string)time();
        $re['nonceStr'] = 'jinbiao';
        $re['url'] = 'www.baidu.com';
        if(empty($_COOKIE['ticket'])) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$re['appid']}&secret={$re['secret']}";
            $data = file_get_contents($url);
            $data = json_decode($data, true);
            $access_token = $data['access_token'];
            if (!empty($data['access_token'])) {
                setcookie('access_token', $data['access_token'], time() + $data['expires_in']);
                $access_token = $data['access_token'];
            }
            $url2 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
            $jsapi_ticket =  file_get_contents($url2);
            $jsapi_ticket =  json_decode($jsapi_ticket, true);
            if (!empty($jsapi_ticket['ticket'])) {
                setcookie('ticket', $jsapi_ticket['ticket'], time() + $jsapi_ticket['expires_in']);
                $re['jsapi_ticket'] = $jsapi_ticket['ticket'];
            }
        }else{
            $re['jsapi_ticket'] = $_COOKIE['ticket'];
        }
        $str = "jsapi_ticket={$re['url']}&noncestr={$re['url']}&timestamp={$re['url']}&url={$re['url']}";
        $re['signature'] =sha1($str);
        return $re;
    }


}
